package com.share.thread.future;

import java.util.concurrent.CompletableFuture;

/**
 *
 * @author caifan
 * @since 2022/6/22
 */
public class CompletableFutureTeaDemo {
    public static void main(String[] args) {
        // task1  洗水壶->烧开水
        CompletableFuture<Void> f1 = CompletableFuture.runAsync(() -> {
            System.out.println("T1:洗水壶...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        //任务2：洗茶壶->洗茶杯->拿茶叶
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(()->{
                    try {
                        System.out.println("T2:洗茶壶...");
                        Thread.sleep(1000);

                        System.out.println("T2:洗茶杯...");
                        Thread.sleep(2000);

                        System.out.println("T2:拿茶叶...");
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return "龙井";
                });

        //任务3：任务1和任务2完成后执行：泡茶
        CompletableFuture<String> f3 = f1.thenCombine(f2, (__, tf)->{
                    System.out.println("T1:拿到茶叶:" + tf);
                    System.out.println("T1:泡茶...");
                    return "上茶:" + tf;
                });

        // 任务3执行结果
        System.out.println(f3.join());
    }
}
